package cn.gson.school.controller.jiaoxue;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiaoxue.ClassDao;
import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.xitong.UserDao;
import cn.gson.school.entity.jiaoxue.ClassEntity;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.entity.xitong.UserEntity;
import cn.gson.school.mappers.jiaoxue.StudentMappers;

@Controller
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentMappers studentMappers;
	
	@Autowired
	private StudentDao studentDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private ClassDao classDao;

	// 查询所有班级
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> index(StudentEntity student,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		System.out.println("student:"+student);
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<StudentEntity> list = studentMappers.studentList(student);
		PageInfo<StudentEntity> info = new PageInfo<>(list);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	//学生列表中的转为部长
	@GetMapping("buzhang/{id}")
	public Map<String, Object> buzhang(@PathVariable(name="id") Long id) {
		System.out.println("进入学生studentconroller");
		Map<String, Object> obj=new HashMap<String, Object>();
		obj.put("success", false);
		StudentEntity studentEntity=studentDao.findOne(id);
		Long classId=studentEntity.getClassId();
		ClassEntity classEntity=classDao.findOne(studentEntity.getClassId());
		classEntity.setClassBuZhang(studentEntity.getStuId());
		classDao.save(classEntity);
		obj.put("success", true);
		return obj;
	}
		//将该学生的状态由在读中改成就业中
		@GetMapping("biye/{id}")
		public Map<String, Object> biye(@PathVariable(name="id") Long id) {
			System.out.println("进入学生studentconroller毕业");
			System.out.println(id);
			Map<String, Object> obj=new HashMap<String, Object>();
			obj.put("success", false);
			StudentEntity studentEntity=studentDao.findOne(id);
			System.out.println("毕业学生信息："+studentEntity);
			studentEntity.setState(Integer.toString(3));
			studentDao.save(studentEntity);
			obj.put("success", true);
			return obj;
		}
		
	
	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> addStudent(@Valid StudentEntity student, HttpServletRequest request, Model model,
			BindingResult br) {
		System.out.println("*****************************************8");
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("errors", br.getFieldErrors());
		} else {
			String id = request.getParameter("studentId");
			String studentName = request.getParameter("studentName");
			String className=request.getParameter("className");
			String sex=request.getParameter("sex");
			String IdCard=request.getParameter("IdCard");
			String StuPhone=request.getParameter("StuPhone");
			String schrollNum=request.getParameter("schrollNum");//学籍号
			String fatherName=request.getParameter("fatherName");
			String fatherPhone=request.getParameter("fatherPhone");
			String motherName=request.getParameter("motherName");
			String motherPhone=request.getParameter("motherPhone");
			String province=request.getParameter("s_province");
			String city=request.getParameter("s_city");
			String county=request.getParameter("s_county");
			String addAddress=request.getParameter("addAddress");
			if ("省份".equals(province)||"地级市".equals(city)||"市、县级市".equals(county)) {
				obj.put("message", "必须要选择一个省市县作为地址");
				return obj;
			}
			if (StuPhone!=null||!"".equals(StuPhone)) {
				if (StuPhone.length()!=11) {
					obj.put("message", "你的手机号必须是11位。。");
					return obj;
				}
			}
			if (fatherPhone!=null||!"".equals(fatherPhone)) {
				if (fatherPhone.length()!=11) {
					obj.put("message", "父亲手机号必须是11位。。");
					return obj;
				}
			}
			if (motherPhone!=null||!"".equals(motherPhone)) {
				if (motherPhone.length()!=11) {
					obj.put("message", "母亲手机号必须是11位。。");
					return obj;
				}
			}
			if (IdCard.length()!=18) {
				obj.put("message", "身份证必须是18位。。");
				return obj;
			}
			StudentEntity studentEntity;
			System.out.println();
			if (id ==null||"".equals(id)) {
				//添加
				System.out.println("添加");
				studentEntity=new StudentEntity();
				UserEntity userEntity=new UserEntity();
				userEntity.setRegistTime(new Date());
				userEntity.setUserAccount(studentName);
				userEntity.setUserPass(DigestUtils.md5Hex("123456789"));
				userDao.save(userEntity);
				System.out.println("添加到user中成功！");
				Long classId=Long.valueOf(className);//有错
				studentEntity.setClassId(classId);
			} else {
				//编辑
				studentEntity=studentDao.findOne(Long.valueOf(id));
				studentEntity.setStuId(Long.valueOf(id));
				System.out.println("编辑");
			}
			UserEntity user=userDao.findByUserAccount(studentName);
			studentEntity.setUserId(user);
			studentEntity.setStudentName(studentName);
			studentEntity.setGender(Integer.valueOf(sex));
			studentEntity.setIdcard(IdCard);
			studentEntity.setContactNum(StuPhone);
			studentEntity.setParentPhone(fatherPhone+"/"+motherPhone);
			studentEntity.setParentName(fatherName+"/"+motherName);
			studentEntity.setAddress(province+city+county+addAddress);
			studentEntity.setEntranceTime(new Date());
			SimpleDateFormat df=new SimpleDateFormat("yyyy");
			int year=Integer.valueOf(IdCard.substring(6, 10));
			int age=Integer.valueOf(df.format(new Date()))-year;
			System.out.println("身份证："+IdCard);
			System.out.println("获取的年份："+year);
			System.out.println("年龄："+age);
			studentEntity.setSchrollNum(schrollNum);
			studentEntity.setAge(age);
			studentEntity.setDelete(1);
			studentEntity.setState("1");
			studentEntity.setTotalCredits(0);
			studentDao.save(studentEntity);
			System.out.println("添加到学生表成功！");
			obj.put("success", true);
		}
		return obj;
	}

}
